package com.wondersgroup.insurance.dnuilearn.views.paint;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;


/**
 * Created by John on 2017/5/8.
 */

public class CircleProgressBar extends View{
    private int max;
    private int roundColor;
    private int roundProgressColor;
    private int textColor;
    private float textSize;
    private float roundWidth;
    private boolean textShow;
    private int progress;
    private Paint paint;
    public static final int STROKE = 0;
    public static final int FILL = 1;

    public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
        max = typedArray.getInteger(R.styleable.CustomProgressBar_max, 100);
        roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.RED);
        roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor, Color.BLUE);
        textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor, Color.GREEN);
        textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize, 55);
        roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth, 10);
        textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow, true);


        typedArray.recycle();
        if (progress > max){
            progress = max;
        }else if (progress < 0){
            progress = 0;
        }
    }

//    @Override   demo
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//
//        //画背景圆环
//        int center = getWidth() / 2;
//
//        float radius = center - roundWidth / 2;
//        paint.setColor(roundColor);
//        paint.setStyle(Paint.Style.STROKE);
//        paint.setStrokeWidth(roundWidth); // 圆环的宽度
//        paint.setAntiAlias(true);
//        canvas.drawCircle(center,center,radius,paint);
//
//        // 画进度百分比
//        paint.setColor(textColor);
//        paint.setStrokeWidth(0);
//        paint.setTextSize(textSize);
//        paint.setTypeface(Typeface.DEFAULT_BOLD);
//
//        int percent = (int)(progress / (float)max * 100);
//        String strPercent = percent + "%";
//        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
//        if(percent != 0){
//            canvas.drawText(strPercent, getWidth() / 2 - paint.measureText(strPercent) / 2 ,
//                    getWidth() / 2  +(fm.bottom - fm.top)/2 - fm.bottom, paint);
//        }
//
//
//        // 画圆弧
//        RectF oval = new RectF(center - radius, center - radius,
//                center + radius, center + radius);
//        paint.setColor(roundProgressColor);
//        paint.setStrokeWidth(roundWidth);
//        paint.setStyle(Paint.Style.STROKE);
//        paint.setStrokeCap(Paint.Cap.ROUND);
//        canvas.drawArc(oval, 0 , 360 * progress / max, false, paint);
//
//    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int center = getWidth() /2;
        paint.setColor(roundColor);
        paint.setStrokeWidth(roundWidth);
        paint.setStyle(Paint.Style.STROKE);
        int radius = (int) (center - roundWidth/2);
        canvas.drawCircle(center,center,radius,paint);



        if (progress >0){


            paint.setColor(roundProgressColor);
            paint.setStrokeCap(Paint.Cap.ROUND);
            RectF rectF = new RectF(center-radius, center-radius,center+radius,center+radius);

            canvas.drawArc(rectF,-90,progress * 360/max,false,paint);
        }


        if (textShow){
            paint.setColor(textColor);
            paint.setStrokeWidth(0);
            paint.setTextSize(textSize);
             String str = "进度"+ progress  +  "%";

            // 通过  取textbounds   取到字体的矩形
//            Rect textReact = new Rect();
//            paint.getTextBounds(str,0,str.length(),textReact);
//            canvas.drawText(str,center -textReact.width()/2, center + textReact.height()/2,paint);

            //FontMetricsInt
       /*     public static class FontMetricsInt {
                public int top;
                public int ascent;
                public int descent;
                public int bottom;
                public int leading;
            }*/

            float textWidth = paint.measureText(str);
            Paint.FontMetricsInt fm = paint.getFontMetricsInt();

            //baseline = center - (fm.top+ fm.bottom)/2;
            canvas.drawText(str, center - textWidth/2,center-(fm.bottom+fm.top)/2,paint);

        }

    }

    public void setProgress(int progress){
        if(progress < 0 ){
            throw new IllegalArgumentException("进度Progress不能小于0");
        }
        if(progress > max){
            progress = max;
        }
        if(progress <= max){
            this.progress = progress;
            postInvalidate();
        }
    }
}
